<?php

namespace app\api\model;

use think\Model;

class Company extends Model
{
    protected $login_rule =   [
        'username'   => 'require|between:5-15',
        'password'   => 'require|between:5-15',
    ];

    protected $login_message  =   [
        'username.require' => '名称必须',
        'username.between' => '账号长度在5-15之间',
        'password.require' => '请输入登录密码',
        'password.between' => '密码长度在5-15之间'
    ];

    public function login_vali($data)
    {

        try {

            validate($this->login_rule,$this->login_message)->check($data);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }

    protected $add_rule =   [
        'name'       => 'require',
        'credit_code'=> 'require',
        'username'   => 'require',
        'password'   => 'require',
        'user_phone' => 'require',
        'key'        =>'require'
    ];

    protected $add_message  =   [
        'name.require' => '请输入单位名称',
        'credit_code.require' => '请输入单位社会信用代码',
        'username.require' => '名称必须',
        'password.require' => '请输入登录密码',
        'user_phone.require' => '请输入联系电话',
        'key.require' => '加密字符错误'
    ];

    public function add_vali($data)
    {
        $info = $this->where('username',$data['username'])->find();
        if($info){
            return re_warning('已经存在该账号');
        }

        try {
            validate($this->add_rule,$this->add_message)->check($data);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function add($data)
    {
        $this->startTrans();
        try {

            $this->allowField([
                'logo',
                'name',
                'credit_code',
                'credit_files',
                'corporation',
                'corporation_code',
                'corporation_files',
                'license_code',
                'company_type',
                'license_files',
                'business_address',
                'phone',
                'user_phone',
                'username',
                'password',
                'key',
                'address',
                'time',
                'passed',
                'check',
                'content',
                'user_id',
                'status'

            ])->save($data);
            $this->commit();
            return re_success("添加成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            return re_error($e->getMessage());
        }
    }

    protected $edit_rule =   [
        'id'   => 'require',
    ];

    protected $edit_message  =   [
        'id.require'=>'缺失的用户',
    ];

    public function edit_vali($data)
    {

        try {
            validate($this->edit_rule,$this->edit_message)->check($data);
//            $info = $this->where('username',$data['username'])->find($data['id']);
//            if(!$info){
//                return re_error('非法操作，已被记录');
//            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }

    public function edit($data)
    {

        $model = Company::find($data['id']);
        $model->startTrans();
        try {
            $model->allowField([
                'logo',
                'name',
                'credit_code',
                'credit_files',
                'corporation',
                'corporation_code',
                'corporation_files',
                'license_code',
                'license_files',
                'business_address',
                'phone',
                'user_phone',
                'username',
                'company_type',
                'password',
                'key',
                'address',
                'passed',
                'content',
                'user_id',
                'check',
                'passed_content',
                'characteristic_address',
                'status'
            ])->save($data);
            $model->commit();

            return re_success("成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
    public function editStatus($data)
    {

        $model = Company::find($data['id']);
        $model->startTrans();
        try {
            $model->allowField([
                'status'
            ])->save($data);
            $model->commit();
            return re_success("操作成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
    /*
     * 企业修改数据详细
     */
    public function companyEdit($data)
    {
        $model = Company::find($data['id']);
        $model->startTrans();
        try {
            $data['check'] = 0;
            $data['status'] = 0;
            $data['check'] = 0;
            $model->allowField([
                'business_license',
                'business_files',
                'corporation',
                'corporation_files',
                'phone',
                'user_phone',
                'password',
                'logo',
                'key',
                'address',
                'passed',
                'check',
                'characteristic_address',
                'status',
                'content'
            ])->save($data);
            $model->commit();
            return re_success("修改成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
    protected $dele_rule =   [
        'id'   => 'require',
    ];

    protected $dele_message  =   [
        'id.require'=>'缺失的企业',
    ];
    public function dele_vali($data)
    {
        try {
            validate($this->dele_rule,$this->dele_message)->check($data);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function dele($id)
    {

        $model = Company::find($id);
        $model->startTrans();
        try {
            $model->delete();
            $model->commit();
            return re_success("删除成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
}